/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: SP.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.baselib.utils;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import io.bhex.baselib.core.CApplication;


public class SP {
    private static final String assertHint = "SP使用前必须初始化，请先调用init函数";
    private static SharedPreferences sp;

    private static SharedPreferences.Editor editor;

    private static void init() {
        if (editor != null)
            return;
        
        sp = PreferenceManager.getDefaultSharedPreferences(CApplication.getInstance());
        editor = sp.edit();
    }

    public static void set(String key, int value) {
        init();
        editor.putInt(key, value).apply();
    }

    public static void set(String key, String value) {
        init();
        sp.edit().putString(key, value).apply();
    }

    public static void set(String key, boolean value) {
        init();
        sp.edit().putBoolean(key, value).apply();
    }

    public static void set(String key, float value) {
        init();
        sp.edit().putFloat(key, value).apply();
    }

    public static void set(String key, long value) {
        init();
        sp.edit().putLong(key, value).apply();
    }

    public static int get(String key, int def) {
        init();
        return sp.getInt(key, def);
    }

    public static boolean get(String key, boolean def) {
        init();
        return sp.getBoolean(key, def);
    }

    public static String get(String key, String def) {
        init();
        return sp.getString(key, def);
    }

    public static float get(String key, float def) {
        init();
        return sp.getFloat(key, def);
    }

    public static long get(String key, long def) {
        init();
        return sp.getLong(key, def);
    }

    public static void remove(String key) {
        init();
        editor.remove(key).apply();
    }

    public static boolean removeExt(String key) {
        init();
        return editor.remove(key).commit();
    }
}
